package com.tbit.uqbike.client.pojo.vo;

import com.fasterxml.jackson.annotation.JsonIgnore;
import com.tbit.uqbike.client.pojo.Coupon;
import com.tbit.uqbike.client.pojo.RideCardUser;
import com.tbit.uqbike.client.pojo.RideLogDetails;
import com.tbit.uqbike.client.pojo.VipCard;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.Accessors;

/**
 * 未支付订单使用卡券抵扣后的费用详情
 *
 * @author <a href="mailto:chen.wutian@tbit.com.cn">BUGTian</a>
 * @since 2025/5/21
 */
@Data
@AllArgsConstructor
@NoArgsConstructor
@Accessors(chain = true)
public class UnpaidOrderUseCarFee {

    /**
     * 可抵扣费用（单位：分）
     */
    private Long canDeductionMoney;

    /**
     * 抵扣后应付费用
     */
    private Long deductionEndMoney;
    /**
     * 待支付金额（订单应付金额 - 用户余额）
     */
    private Long waitingPayMoney;
    /**
     * 用户可用余额
     */
    private Long userMoney;
    /**
     * 用户余额抵扣金额
     */
    private Long userMoneyDeduction;

    /**
     * 计算用户待支付金额
     * 待支付金额 = 订单应付金额 - 用户余额
     * 用户余额抵扣金额 = 如果用户余额大于等于订单应付金额-> 订单应付金额，如果用户余额小于订单应付金额->用户余额
     * @param userMoney 用户当前余额
     */
    public void computeWaitingPayMoney(Long userMoney) {
        this.userMoney = userMoney;
        waitingPayMoney = deductionEndMoney - userMoney < 0 ? 0 : deductionEndMoney - userMoney;
        userMoneyDeduction = userMoney >= deductionEndMoney ? deductionEndMoney : userMoney;
    }

    /**
     * 临时变量存储骑行订单，防止重复查库
     */
    @JsonIgnore
    private RideLogDetails rideLogDetails;

    /**
     * 抵扣的卡券类型
     */
    @JsonIgnore
    private Integer deductionCardType;

    /**
     * 当前抵扣使用的卡券号
     */
    @JsonIgnore
    private String cardNO;

    /**
     * 临时变量存储骑行卡信息，防止重复查库
     */
    @JsonIgnore
    private RideCardUser rideCardUser;
    /**
     * 临时变量存储会员卡信息，防止重复查库
     */
    @JsonIgnore
    private VipCard vipCard ;
    /**
     * 临时变量存储优惠券信息，防止重复查库
     */
    @JsonIgnore
    private Coupon coupon;

    public UnpaidOrderUseCarFee(Long canDeductionMoney, Long deductionEndMoney, RideLogDetails rideLogDetails) {
        this.canDeductionMoney = canDeductionMoney;
        this.deductionEndMoney = deductionEndMoney;
        this.rideLogDetails = rideLogDetails;
    }

    /**
     * 计算抵扣后应付骑行费用
     * @param rideMoney 骑行金额
     * @return 当前费用详情对象
     */
    public UnpaidOrderUseCarFee computeDeductionEndMoney(long rideMoney) {
        this.deductionEndMoney = rideMoney - canDeductionMoney;
        return this;

    }

}
